package com.cskaoyan.service.wxfootprint;

import com.cskaoyan.bean.Footprint;
import com.cskaoyan.bean.FootprintExample;
import com.cskaoyan.bean.good.Goods;
import com.cskaoyan.bean.User1;
import com.cskaoyan.bean.wxfootprint.vo.WxFootprintListVo;
import com.cskaoyan.bean.wxfootprint.vo.WxFootprintListVoFootprint;
import com.cskaoyan.mapper.FootprintMapper;
import com.cskaoyan.mapper.GoodsMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class WxFootprintServiceImp implements WxFootprintService {
    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    GoodsMapper goodsMapper;
    @Override
    public WxFootprintListVo listUserFootprint(Integer page, Integer size) {
        Subject subject = SecurityUtils.getSubject();
        User1 user = (User1) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();
        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andDeletedEqualTo(false);
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        Collections.reverse(footprints);
        WxFootprintListVo wxFootprintListVo = new WxFootprintListVo();
        wxFootprintListVo.setTotalPages(footprints.size()/size + 1);
        ArrayList<WxFootprintListVoFootprint> wxFootprintListVoFootprints = new ArrayList<>();
        for (int i = (page -1)*10 ; i < page * 10 ; i++) {
            if (i > footprints.size() - 1) {
                break;
            }
            Footprint footprint = footprints.get(i);
            Goods goods = goodsMapper.selectByPrimaryKey(footprint.getGoodsId());
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ss HH:mm:ss");
            String format = dateFormat.format(footprint.getAddTime());
            WxFootprintListVoFootprint wxFootprintListVoFootprint = new WxFootprintListVoFootprint(
                    goods.getBrief(), goods.getPicUrl(),
                    format,goods.getId(), goods.getName(), footprint.getId(),goods.getRetailPrice());
            wxFootprintListVoFootprints.add(wxFootprintListVoFootprint);
        }
        wxFootprintListVo.setFootprintList(wxFootprintListVoFootprints);
        return wxFootprintListVo;
    }

    @Override
    public Integer deleteFootPrint(Integer id) {
        Footprint footprint = new Footprint();
        footprint.setDeleted(true);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDate = dateFormat.format(new Date());
        footprint.setUpdateTime(new Date());
        footprint.setId(id);
        Integer result = footprintMapper.updateByPrimaryKeySelective(footprint);
        return result;
    }
}
